package com.kitty.mina.codec;


import com.kitty.mina.message.Message;
import com.kitty.mina.session.SessionProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.nutz.json.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//后加
import com.kitty.mina.codec.CodecContext;
import com.kitty.mina.codec.IMessageEncoder;
import com.kitty.mina.codec.SerializerHelper;


public class MinaEncoder implements ProtocolEncoder {

    @Override
    public void dispose(IoSession arg0) throws Exception {

    }

    @Override
    public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
        CodecContext context = (CodecContext) session.getAttribute(SessionProperties.CODEC_CONTEXT);
        if (context == null) {
            context = new CodecContext();
            session.setAttribute(SessionProperties.CODEC_CONTEXT, context);
        }
        IoBuffer buffer = writeMessage((Message) message);
        out.write(buffer);
    }

    public static IoBuffer writeMessage(Message message) {
        int moduleId = message.getModule();
        IoBuffer buffer = IoBuffer.allocate(32).setAutoExpand(true).setAutoShrink(true);
        buffer.putUnsignedInt(1297743872);
        buffer.putUnsignedInt(0);
        //写入具体消息的内容
        IMessageEncoder msgEncoder = SerializerHelper.getInstance().getEncoder();
        if (16383 != moduleId&& 4275 != moduleId){
//            logger.error("返回解析协议:==={}=={}", moduleId, Json.toJson(message));
        }
        byte[] body = msgEncoder.writeMessageBody(message);
        // 包体长度
        int posi = 0;
        if (moduleId == 64977) {
            posi = body.length + 4;
        } else {
            posi = body.length + 2;
        }
        buffer.putShort((short) posi);
        buffer.putUnsignedShort(moduleId);
        if (moduleId == 64977) {
            buffer.putShort((short) ((short) body.length + 2));
        }
        buffer.put(body);
        buffer.flip();
        buffer.rewind();
        if (16383 != moduleId && 4275 != moduleId){
//            logger.error("返回解析协议:==={}=={}", moduleId, buffer.getHexDump());
        }
        return buffer;
    }

}
